import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-08
 * Time: 21:48
 */
public class Main {
    //自己写的
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> lists = new ArrayList<>();
        Arrays.sort(nums);
        int n = nums.length;

        for (int i = 0; i < n - 3; i++) {

            if (i != 0 && nums[i] == nums[i - 1]) {
                continue;
            }


            for (int j = i + 1; j < n - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }

                int left = j + 1;
                int right = n - 1;
                while (left < right){
                    long sum = (long)nums[i] + (long)nums[j] + (long)nums [left] + (long)nums[right];
                    if (sum > target){
                        right--;
                    } else if (sum < target) {
                        left++;
                    }else {
                        List<Integer> list = new ArrayList<>(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        lists.add(list);

                        while (left < right && nums[left] == nums[left+1]){
                            left++;
                        }
                        while (left < right && nums[right] == nums[right-1]){
                            right--;
                        }

                        right--;left++;
                    }
                }
            }
        }
        return lists;
    }

    //老师写的
    public List<List<Integer>> fourSum1(int[] nums, int target) {
        List<List<Integer>> ret = new ArrayList<>();

        //1.排序
        Arrays.sort(nums);

        //2.利用双指针解决问题
        int n = nums.length;
        for (int i = 0; i < n;) { // 固定数a
            //三数之和
            for (int j = i + 1; j < n;) { //固定数b
                //双指针
                int left = j + 1;
                int right = n - 1;
                long aim = (long) target - (long) nums[i] - (long) nums[j];
                while (left < right){
                    long sum = (long) nums[left] + (long) nums[right];
                    if (sum > aim){
                        right--;
                    }else if (sum < aim){
                        left++;
                    }else {
                        ret.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        left++;
                        right--;
                        //去重
                        while (left < right && nums[left] == nums[left-1]){
                            left++;
                        }
                        while (left < right && nums[right] == nums[right+1]){
                            right--;
                        }
                    }
                }
                //去重二
                j++;
                while (j < n && nums[j] == nums[j-1]){
                    j++;
                }
            }
            //去重
            i++;
            while (i < n && nums[i] == nums[i - 1]){
                i++;
            }
        }
        return ret;
    }
}
